<!-- 地址 -->
<template>
  <div class="checkout-address">
    <div class="text">
      <div v-if="!addlist" class="none">您需要先添加收货地址才可提交订单。</div>
      <ul v-if="addlist">
        <li>
          <span>收<i />货<i />人：</span>{{ addlist.receiver }}
        </li>
        <li><span>联系方式：</span>{{ addlist.contact }}</li>
        <li><span>收货地址：</span>{{ addlist.address }}</li>
      </ul>
      <a v-if="addlist" href="javascript:;">修改地址</a>
    </div>
    <div class="action">
      <button class="btn" @click="show2">切换地址</button>
      <button class="btn" @click="show3">添加地址</button>
    </div>
    <confirm title="切换收货地址" v-show="dialogVisible" @quxiao="quxiao">
      <template>
        <div
          class="text item"
          v-for="(item, key) in list"
          :key="item.id"
          @click="qie(key)"
        >
          <ul>
            <li>
              <span>收<i />货<i />人：</span>{{ item.receiver }}
            </li>
            <li><span>联系方式：</span>{{ item.contact }}</li>
            <li><span>收货地址：</span>{{ item.address }}</li>
          </ul>
        </div>
      </template>
    </confirm>

    <confirm v-show="dialogVisible2" title="添加收货地址" @quxiao="quxiao" @queding="queding">
      <AddressEdit :formData="formData"></AddressEdit>
    </confirm>
  </div>
</template>
<script>
import confirm from "./confirm.vue";
import AddressEdit from "./AddressEdit.vue";
import { addresslist ,addlists } from "@/utils/Api/NavText";
export default {
  name: "CheckoutAddress",
  components: { confirm, AddressEdit },
  data() {
    return {
      dialogVisible: false,
      dialogVisible2: false,
      selectedAddress: null,
      list: [],
      addlist: [],
      text: 1,
      formData: {
        receiver: "",
        contact: "",
        provinceCode: "",
        cityCode: "",
        countyCode: "",
        address: "",
        postalCode: "",
        addressTags: "",
        isDefault: 0,
        fullLocation: "",
      },
    };
  },

  async mounted() {
    let res = await addresslist();
    this.list = res.result;
    this.addlist = res.result[0];
    // console.log(res);
  }, 
  methods: {
    show2() {
      this.dialogVisible = true;
    },
    show3() {
      this.dialogVisible2 = true;
    },

    qie(key) {
      this.addlist = this.list[key];
      this.dialogVisible = false;
    },

    quxiao() {
      this.dialogVisible = false;
      this.dialogVisible2 = false;
    },
    queding(){
      addlists(this.formData)
      console.log(this.formData);
    }
  },
};
</script>
<style scoped lang="scss">
@import "@/assets/css/color.scss";
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: $xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,
      &:hover {
        border-color: $xtxColor;
        background: lighten($xtxColor, 50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
</style>
